package admin

import (
	"duoke/model"
	"duoke/utils/gconv"
	"duoke/utils/gtime"
	"duoke/utils/orm"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
)

var CouponList = new(couponListCtl)

type couponListCtl struct{}

func (ctl *couponListCtl) List(c *gin.Context) {
	var req model.ReqCouponList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	where := "id > 0 "
	if req.Keyword != "" {
		where += "AND (user_id ='" + req.Keyword + "' OR order_no ='" + req.Keyword + "' OR nickname ='" + req.Keyword + "' OR name ='" + req.Keyword + "' OR phone ='" + req.Keyword + "' )"
	}
	if req.PayStatus != "" {
		where += "AND pay_status = '" + req.PayStatus + "' "
	}
	if req.CouponId != "" {
		where += "AND coupon_id = '" + req.CouponId + "' "
	}
	if req.MerUserId != "" {
		where += "AND coupon_id = '" + req.MerUserId + "' "
	}
	if req.TimeRange != nil && len(req.TimeRange) == 2 {
		startTime := gtime.StrToTime(req.TimeRange[0])
		endTime := gtime.StrToTime(req.TimeRange[1])
		where += "AND pay_time >= '" + gconv.String(startTime) + "' "
		where += "AND pay_time <= '" + gconv.String(endTime) + "' "
	}

	count, err := orm.Db.Where(where).Table("coupon_order").Count()
	if err != nil || count == 0 {
		response.Error(c, err.Error(), nil)
		return
	}
}
